<script setup lang="ts">
import { useBookStore } from './stores/bookStore';
import BookUpload from './components/BookUpload.vue';
import PlayerView from './views/PlayerView.vue';
import { onMounted, onUnmounted } from 'vue';

const store = useBookStore();

// 键盘事件处理函数
const handleKeyDown = (event: KeyboardEvent) => {
  // 如果当前没有书籍，不处理键盘事件
  if (!store.chapters.length) return;
  
  // 如果焦点在输入框或文本区域，不处理空格键
  const target = event.target as HTMLElement;
  if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.contentEditable === 'true') {
    return;
  }
  
  // 空格键控制播放/暂停/恢复
  if (event.code === 'Space') {
    event.preventDefault(); // 阻止页面滚动
    if (store.isPlaying) {
      store.togglePause();
    } else {
      // 优先尝试恢复，如失败自动重播
      (store as any).safeResumeOrRestart();
    }
  }
};

// 页面可见性/焦点恢复时，同步speech状态
const handleVisibilityOrFocus = () => {
  (store as any).syncSpeechStateFromBrowser?.();
};

onMounted(() => {
  document.addEventListener('keydown', handleKeyDown);
  window.addEventListener('focus', handleVisibilityOrFocus);
  document.addEventListener('visibilitychange', handleVisibilityOrFocus);
});

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeyDown);
  window.removeEventListener('focus', handleVisibilityOrFocus);
  document.removeEventListener('visibilitychange', handleVisibilityOrFocus);
});
</script>

<template>
  <div class="app-container">
    <header class="header">
      <h1>📚 ZY小书呆</h1>
      <p>让阅读更轻松，让听书更可爱</p>
    </header>
    
    <main class="content">
      <BookUpload v-if="!store.chapters.length" />
      <PlayerView v-else />
    </main>
    
    <footer class="footer">
      <p>© 2024 ZY小书呆 | 用心打造最好的听书体验</p>
    </footer>
  </div>
</template>

<style scoped>
.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  padding: 30px 20px;
  text-align: center;
  background: rgba(255, 248, 225, 0.9);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  color: #8d6e63;
  box-shadow: 0 8px 32px rgba(255, 245, 157, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 0 0 30px 30px;
  margin-bottom: 20px;
  animation: slideDown 0.8s ease-out;
}

.header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(255, 245, 157, 0.3);
  background: linear-gradient(135deg, #fdd835, #fff59d);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header p {
  font-size: 1.1rem;
  opacity: 0.9;
  font-weight: 300;
}

.content {
  flex: 1;
  padding: 30px 20px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.footer {
  padding: 20px;
  text-align: center;
  color: #8d6e63;
  font-size: 0.9rem;
  background: rgba(255, 248, 225, 0.8);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid rgba(255, 245, 157, 0.2);
  border-radius: 30px 30px 0 0;
  margin-top: 20px;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .header h1 {
    font-size: 2rem;
  }
  
  .content {
    padding: 20px 15px;
  }
}
</style>
